package icu.cqcai.xinsi.mapper;

import icu.cqcai.xinsi.domain.InterviewAnswer;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 面试回答数据访问接口
 */
@Mapper
public interface InterviewAnswerMapper {
    
    /**
     * 插入面试回答
     */
    @Insert("INSERT INTO interview_answers (user_id, session_id, question_id, question_title, " +
            "question_content, question_category, answer_content, answer_duration, " +
            "ai_score, ai_analysis, strength_points, improvement_points, keyword_match, " +
            "status, create_time, update_time) " +
            "VALUES (#{userId}, #{sessionId}, #{questionId}, #{questionTitle}, " +
            "#{questionContent}, #{questionCategory}, #{answerContent}, #{answerDuration}, " +
            "#{aiScore}, #{aiAnalysis}, #{strengthPoints}, #{improvementPoints}, #{keywordMatch}, " +
            "#{status}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertAnswer(InterviewAnswer answer);
    
    /**
     * 根据用户ID和会话ID查询回答
     */
    @Select("SELECT * FROM interview_answers WHERE user_id = #{userId} AND session_id = #{sessionId} " +
            "AND status = 1 ORDER BY question_id ASC")
    List<InterviewAnswer> selectAnswersByUserAndSession(@Param("userId") Long userId, 
                                                       @Param("sessionId") String sessionId);
    
    /**
     * 根据用户ID和会话ID查询所有回答记录（包括未回答的）
     */
    @Select("SELECT * FROM interview_answers WHERE user_id = #{userId} AND session_id = #{sessionId} " +
            "ORDER BY question_id ASC")
    List<InterviewAnswer> selectAllAnswersByUserAndSession(@Param("userId") Long userId, 
                                                          @Param("sessionId") String sessionId);
    

    /**
     * 更新回答内容和AI分析
     */
    @Update("UPDATE interview_answers SET answer_content = #{answerContent}, " +
            "answer_duration = #{answerDuration}, ai_score = #{aiScore}, " +
            "ai_analysis = #{aiAnalysis}, strength_points = #{strengthPoints}, " +
            "improvement_points = #{improvementPoints}, keyword_match = #{keywordMatch}, " +
            "update_time = #{updateTime} WHERE id = #{id}")
    int updateAnswer(InterviewAnswer answer);
    

    /**
     * 根据用户ID查询所有回答历史
     */
    @Select("SELECT * FROM interview_answers WHERE user_id = #{userId} AND status = 1 " +
            "ORDER BY create_time DESC LIMIT #{offset}, #{limit}")
    List<InterviewAnswer> selectAnswersByUserId(@Param("userId") Long userId, 
                                              @Param("offset") int offset, 
                                              @Param("limit") int limit);
    


    /**
     * 检查用户是否已回答某题目
     */
    @Select("SELECT COUNT(*) > 0 FROM interview_answers WHERE user_id = #{userId} " +
            "AND session_id = #{sessionId} AND question_id = #{questionId} AND status = 1")
    boolean existsAnswer(@Param("userId") Long userId, 
                        @Param("sessionId") String sessionId, 
                        @Param("questionId") Integer questionId);
} 